package com.gewei.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.gewei.dao.MemberDao;
import com.gewei.dao.OrderDao;
import com.gewei.service.ReportService;
import com.gewei.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 运营数据统计服务
 *
 * @author gewei
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    /**
     * 查询运营数据
     * Map数据格式：
     *      todayNewMember -> number
     *      totalMember -> number
     *      thisWeekNewMember -> number
     *      thisMonthNewMember -> number
     *      todayOrderNumber -> number
     *      todayVisitsNumber -> number
     *      thisWeekOrderNumber -> number
     *      thisWeekVisitsNumber -> number
     *      thisMonthOrderNumber -> number
     *      thisMonthVisitsNumber -> number
     *      hotSetmeals -> List<Setmeal>
     *
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> getBusinessReportData() throws Exception {

        //  运营数据
        Map<String, Object> result = new HashMap<>();

        //  报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        result.put("reportDate", today);
        //  获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //  获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //  会员数据统计
        //  本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        result.put("todayNewMember", todayNewMember);
        //  总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        result.put("totalMember", totalMember);
        //  本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        result.put("thisWeekNewMember", thisWeekNewMember);
        //  本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);
        result.put("thisMonthNewMember", thisMonthNewMember);

        //  预约到诊数据统计
        //  今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        result.put("todayOrderNumber", todayOrderNumber);
        //  本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        result.put("thisWeekOrderNumber", thisWeekOrderNumber);
        //  本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        result.put("thisMonthOrderNumber", thisMonthOrderNumber);

        //  今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        result.put("todayVisitsNumber", todayVisitsNumber);
        //  本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        result.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        //  本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);
        result.put("thisMonthVisitsNumber", thisMonthVisitsNumber);

        //  热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();
        result.put("hotSetmeal", hotSetmeal);

        return result;
    }
}